const express = require('express');
const db = require('../uilit/mysql');


const router = express.Router();


router.post('/register', function(req, res) {
    const sql1 = `select * from seeker where s_tel='${req.body.username}';`;
    const sql2 = `select * from company where c_tel='${req.body.username}';`;
    const sql3 = `INSERT INTO seeker (s_tel,s_password) VALUES('${req.body.username}','${req.body.password}');`;
    const sql4 = `INSERT INTO company (c_name,c_hrname,c_tel,c_password,c_hremail) VALUES('${req.body.name}','${req.body.hrname}',
    '${req.body.username}','${req.body.password}','${req.body.hremail}');`;

    if (req.body.Id == 1) {
        db.query(sql1, function(err1, data1) {
            if (err1) {
                console.log(err1)
            } else {
                if (data1.length) {
                    res.send('该用户已存在!')
                } else {
                    db.query(sql3, function(err, data) {
                        if (err) {
                            console.log(err)
                        } else {
                            
                                res.send('注册成功!')
                           
                        }
                    })
                }
            }
        })

    }


    if (req.body.Id == 2) {
        db.query(sql2, function(err2, data2) {
            if (err2) {
                console.log(err2)
            } else {
                if (data2.length) {
                    res.send('该用户已存在!')
                } else {
                    db.query(sql4, function(err, data) {
                        if (err) {
                            console.log(err)
                        } else {
                            if (data.length) {
                                res.send('注册成功!')
                            }
                        }
                    })
                }
            }
        })
    }

})
module.exports = router;